package com.loe.ui.view_other

import android.content.Context
import android.view.View
import android.webkit.JavascriptInterface
import androidx.annotation.Keep
import com.loe.ui.*
import com.loe.ui.api.TextViewApi
import com.loe.ui.page.UiPage
import com.loe.ui.quickjs.jsColor
import com.loe.ui.view.UiTextView
import com.quickjs.JSArray
import com.quickjs.JSObject

@Keep
class UiCheckText(context: Context) : UiTextView(context)
{
    init
    {
        setOnClickListener()
        {
            if (isCheckEnable)
            {
                toggle()
            }
        }
    }

    override val jsObject by lazy {
        val view = this
        object : TextViewApi(this)
        {
            @JavascriptInterface
            fun setEnable(isEnable: Boolean)
            {
                view.isCheckEnable = isEnable
            }

            @JavascriptInterface
            fun setCheck(isCheck: Boolean)
            {
                view.isCheck = isCheck
            }
        }.jsObject
    }

    override fun setUI(ui: JSObject, uiPage: UiPage?): View
    {
        super.setUI(ui, uiPage)

        // checkImage
        ui.gotHas("checkImage0") { checkImage0 = it }
        ui.gotHas("checkImage1") { checkImage1 = it }
        ui.gotHas("checkImage2") { checkImage2 = it }

        // checkColor
        ui.gotHasString("checkColor0") { checkColor0 = it }
        ui.gotHasString("checkColor1") { checkColor1 = it }
        ui.gotHasString("checkColor2") { checkColor2 = it }

        imageTop = ui.gotString("imageTop", "0.5").px
        imageWidth = ui.gotString("imageWidth", "14").px
        imageHeight = ui.gotString("imageHeight", "14").px
        imagePadding = ui.gotString("imagePadding", "5").px
        imageDirect = ui.gotString("imageDirect")

        onCheck = null
        // isCheckEnable
        ui.gotHasBoolean("isCheckEnable") { isCheckEnable = it }
        // isCheck
        isCheck = ui.gotBoolean("isCheck")
        // onCheck
        ui.gotHasFunction("onCheck")
        { callback ->
            onCheck = { callback.call(jsObject, JSArray(jsContext).push(it)) }
        }

        return this
    }

    private var checkImage0: Any = "icon_check_0"
    private var checkImage1: Any = "icon_check_1"
    private var checkImage2: Any = "icon_check_2"

    private var checkColor0 = ""
    private var checkColor1 = jsColor.main
    private var checkColor2 = jsColor.gray

    fun toggle()
    {
        isCheck = !isCheck
    }

    fun notifyCheck()
    {
        if (isCheckEnable)
        {
            if (isCheck)
            {
                image = checkImage1
                imageColor = checkColor1
            } else
            {
                image = checkImage0
                imageColor = checkColor0
            }
        } else
        {
            if (isCheck)
            {
                image = checkImage2
                imageColor = checkColor2
            } else
            {
                image = checkImage0
                imageColor = checkColor0
            }
        }
        notifyImage()
    }

    var isCheckEnable = true
        set(value)
        {
            field = value
            notifyCheck()
        }

    var isCheck = false
        set(value)
        {
            field = value
            notifyCheck()
            onCheck?.invoke(isCheck)
        }

    private var onCheck: ((isCheck: Boolean) -> Unit)? = null

    fun setOnCheck(onCheck: (isCheck: Boolean) -> Unit)
    {
        this.onCheck = onCheck
    }
}